package com.summer.service.impl;

import com.summer.entity.UserContactDetail;
import com.summer.mapper.UserContactDetailDAO;
import com.summer.mapper.UserHuluDAO;
import com.summer.mapper.UserInfoMapper;
import com.summer.service.IUserContactDetailService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by evalcony on 2017/12/20.
 */
@Service
@Slf4j
public class UserContactDetailService implements IUserContactDetailService {

    @Resource
    private UserContactDetailDAO userContactDetailDAO;

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserHuluDAO userHuluDAO;


    @Override
    public boolean insertIfNotExist(int userId) {
        int jxlUserContactCount = userContactDetailDAO.countExist(userId);
        if (jxlUserContactCount == 0) {
            try {
                List<UserContactDetail> jxlDataList = analyseUserJXLReport(userId);
                if (CollectionUtils.isEmpty(jxlDataList)) {
                    log.error("jxl解析出来数据的list is empty");
                    return false;
                }
                userContactDetailDAO.insertBatch(jxlDataList);
            } catch (Exception e) {
                log.error("插入用户jxl报告通话统计数据报错", e);
                return false;
            }
        }
        return true;
    }



    /**
     * 解析jxl报告，获得用户的所有通话统计数据
     * @param userId
     * @return
     */
    private List<UserContactDetail> analyseUserJXLReport(Integer userId) {

        List<UserContactDetail> list = new ArrayList<UserContactDetail>();

        if (userId == null) {
            return list;
        }

        String JXL_DETAIL = userHuluDAO.getJxlDetail(userId);
        if (StringUtils.isBlank(JXL_DETAIL)) {
            System.out.println("JXL_DETAIL is empty");
            return list;
        }

        JSONObject jxlJson = null;
        try {
            jxlJson = JSONObject.fromObject(JXL_DETAIL.toString());

        } catch (Exception e) {
            log.error("jxl detail json 解析错误", e);
            return list;
        }

        JSONObject jxlData = jxlJson.getJSONObject("data");

        JSONArray contactArray = jxlData.getJSONArray("contact_list");
        for (int i = 0; i < contactArray.size(); ++i) {
            JSONObject contactJson = contactArray.getJSONObject(i);

            UserContactDetail userContact = null;
            try {
                userContact = analyseUserJXLSingleInfo(contactJson);
            } catch (Exception e) {
                userContact = analyseUserJXLSingleInfo();
                log.error("jxl detail contactJson 解析错误", e);
            }
            userContact.setUserId(userId);
            list.add(userContact);
        }

        return list;
    }

    /**
     * 解析单个数据，返回UserContactDetail对象
     * @param contactJson
     * @return
     */
    private UserContactDetail analyseUserJXLSingleInfo(JSONObject contactJson) {

        UserContactDetail info = new UserContactDetail();

        info.setContactName(contactJson.getString("contact_name"));
        info.setPhoneNum(contactJson.getString("phone_num"));
        info.setPhoneNumLoc(contactJson.getString("phone_num_loc"));
        info.setCallCnt(contactJson.getInt("call_cnt"));
        info.setCallLen(contactJson.getDouble("call_len")+"");
        info.setCallInCnt(contactJson.getInt("call_in_cnt"));
        info.setCallInLen(contactJson.getDouble("call_in_len")+"");
        info.setCallOutCnt(contactJson.getInt("call_out_cnt"));
        info.setCallOutLen(contactJson.getDouble("call_out_len")+"");
        info.setContact1w((short)contactJson.getInt("contact_1w"));
        info.setContact1m((short)contactJson.getInt("contact_1m"));
        info.setContact3m((short)contactJson.getInt("contact_3m"));
        info.setContact3mPlus(contactJson.getInt("contact_3m_plus"));
        info.setContactHoliday((short)contactJson.getInt("contact_holiday"));
        info.setContactWeekday((short)contactJson.getInt("contact_weekday"));
        info.setContactWeekend((short)contactJson.getInt("contact_weekend"));
        info.setContactEarlyMorning((short)contactJson.getInt("contact_early_morning"));
        info.setContactMorning((short)contactJson.getInt("contact_morning"));
        info.setContactNoon((short)contactJson.getInt("contact_noon"));
        info.setContactAfternoon((short)contactJson.getInt("contact_afternoon"));
        info.setContactNight((short)contactJson.getInt("contact_night"));

        return info;
    }
    private UserContactDetail analyseUserJXLSingleInfo() {
        UserContactDetail info = new UserContactDetail();

        info.setContactName("");
        info.setPhoneNum("");
        info.setPhoneNumLoc("");
        info.setCallCnt(0);
        info.setCallLen("0");
        info.setCallInCnt(0);
        info.setCallInLen("0");
        info.setCallOutCnt(0);
        info.setCallOutLen("0");
        info.setContact1w((short)0);
        info.setContact1m((short)0);
        info.setContact3m((short)0);
        info.setContact3mPlus(0);
        info.setContactHoliday((short)0);
        info.setContactWeekday((short)0);
        info.setContactWeekend((short)0);
        info.setContactEarlyMorning((short)0);
        info.setContactMorning((short)0);
        info.setContactNoon((short)0);
        info.setContactAfternoon((short)0);
        info.setContactNight((short)0);

        return info;
    }


}
